package com.xywy.me;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.constants.ConstantValue;
import com.xywy.common.constants.ResultCode;
import com.xywy.common.net.AsyncHttpClient;
import com.xywy.common.net.OnResultListenerAdapter;
import com.xywy.common.net.Parser;
import com.xywy.common.util.MLog;
import com.xywy.common.util.StringUtils;
import com.xywy.common.widget.MyProgress;
import com.xywy.common.widget.TitleBar2;
import com.xywy.me.bean.UserKeeper;

/**
 * @ClassName: SetupVerificationPasswordActivity
 * @Package com.xywy.me
 * @Description: 验证密码
 * @author CodeMonkeyCui
 * @date 2014年11月5日 下午8:00:13
 * @version V1.0
 */

public class SetupVerificationPasswordActivity extends BaseActivity {

	private Button button;
	private ImageView mCancel;
	private Context mContext;
	private EditText et_password;
	private String mobile;
	private int tag;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		// 设置为全屏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_verification_password);

		this.preInit();
		this.initView();
		this.loadData();
		this.setListener();
	}

	@Override
	public void preInit() {
		// TODO Auto-generated method stub
		mContext = this.context;
		Intent intent = this.getIntent();
		mobile = intent.getStringExtra("mobile");
		tag = intent.getIntExtra("TAG", 0);
	}

	@Override
	public void loadData() {
		// TODO Auto-generated method stub

	}

	private void setListener() {
		et_password.addTextChangedListener(mTextWatcher);
	}

	TextWatcher mTextWatcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			// TODO Auto-generated method stub

		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub

		}

		@Override
		public void afterTextChanged(Editable editable) {
			if (editable.length() >= 0) {
				mCancel.setVisibility(View.VISIBLE);
				button.setBackgroundDrawable(mContext.getResources()
						.getDrawable(R.drawable.shape_button));
				button.setClickable(true);
			} else {
				mCancel.setVisibility(View.INVISIBLE);
				button.setBackgroundDrawable(mContext.getResources()
						.getDrawable(R.drawable.shape_button_bgwhite));
				button.setClickable(false);
			}

		}
	};

	@Override
	public void initView() {
		// TODO Auto-generated method stub
		TitleBar2 titleBar = (TitleBar2) findViewById(R.id.activity_me_setup_verification_password_title_bar);
		titleBar.onClickBack(this);

		TextView tvBack = titleBar.getTvBack();
		tvBack.setText(getString(R.string.tv_start_back));

		TextView tvTitle = titleBar.getTitle();
		tvTitle.setText(getString(R.string.me_setup_account_yzmm));

		titleBar.getGo().setVisibility(View.INVISIBLE);

		et_password = (EditText) findViewById(R.id.et_setup_ac_val_psw);
		mCancel = (ImageView) findViewById(R.id.iv_setup_verification_password_mcancel);
		button = (Button) findViewById(R.id.bt_setup_verification_password_next);
	}

	public void startOnClick(View view) {
		switch (view.getId()) {
		case R.id.bt_setup_verification_password_next:
			next();
			break;
		case R.id.iv_setup_verification_password_mcancel:
			et_password.setText("");

		}
	}

	private void next() {
		// TODO Auto-generated method stub
		sendMsg();

	}

	MyProgress myProgress;
	JSONObject msgBack = null;
	/**
	 * 返回
	 */
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			MLog.i(TAG, "back:" + msg.obj + "resuletCode:" + msg.arg1);
			myProgress.stop();
			if (null != msg.obj) {
				try {
					msgBack = new JSONObject(msg.obj.toString());
				} catch (JSONException e) {
					e.printStackTrace();
				}
				if (msg.arg1 == ResultCode.SUCCESS) {
					// Intent intent =new Intent(mContext,
					// SetupValdateCodeActivity.class);
					// intent.putExtra("mobile",mobile);
					// mContext.startActivity(intent);
					if (tag == 1) {

						Intent intent = new Intent(mContext,
								SetupAcValMobileActivity.class);
						intent.putExtra("mobile", mobile);
						mContext.startActivity(intent);
					} else {
						Intent intent = new Intent(mContext,
								SetupAcPwdActivity.class);
						intent.putExtra("mobile", mobile);
						SharedPreferences sp = getSharedPreferences("config",
								Context.MODE_PRIVATE);
						Editor editor = sp.edit();
//						String userid = sp.getString("userid", "");
						String userid = UserKeeper.getCurrentUser(SetupVerificationPasswordActivity.this).getUserid();
						intent.putExtra("userid", userid);
						mContext.startActivity(intent);

					}
					finish();
				} else if (msg.arg1 == -10003) {
					Toast.makeText(mContext, "密码错误",// msgBack.optString("error")
							Toast.LENGTH_LONG).show();
				} else {
					Toast.makeText(mContext, msgBack.optString("error"),
							Toast.LENGTH_LONG).show();
				}
			}
		}
	};
	// 封装Parser参数
	private Parser paramsParser;
	// 封装httpclient参数
	private HashMap<String, Object> params;
	// 异步加载
	private AsyncHttpClient asyncHttpClient;

	// 处理网络请求
	public void sendMsg() {
		myProgress = new MyProgress(this, 10);
		myProgress.start();
		paramsParser = new Parser();

		// 封装参数
		params = new HashMap<String, Object>();

		// 请求路径
		paramsParser.action = getString(R.string.validate_password);
		// 请求方式
		paramsParser.request = "post";

		// 请求参数
		if (StringUtils.isNull(mobile)) {
			SharedPreferences sp = sp = getSharedPreferences("config",
					Context.MODE_PRIVATE);
			mobile = sp.getString("mobile", "");
		}
		params.put("phonenum", mobile.trim());
		params.put("password", et_password.getText().toString());

		paramsParser.map = params;
		// 传递上下文
		paramsParser.context = this.context;

		asyncHttpClient = AsyncHttpClient.getInstance();
		asyncHttpClient.exeute(paramsParser, onResultListenerAdapter);
	}

	OnResultListenerAdapter onResultListenerAdapter = new OnResultListenerAdapter() {
		@Override
		public <T extends Parser> void onSuccess(T resultParser, int resultCode) {
			super.onSuccess(resultParser, resultCode);
			Message message = Message.obtain();
			message.arg1 = resultCode;
			message.obj = resultParser.back;
			handler.sendMessage(message);
		}
	};
}